package com.yh.homestay.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.common.Send;
import com.yh.homestay.dao.HomeStayOrderMapper;
import com.yh.homestay.dao.OrderCarDriverMapper;
import com.yh.homestay.dao.SysOrderMapper;
import com.yh.homestay.pojo.CustomSysOrder;
import com.yh.homestay.pojo.OrderCarDriver;
import com.yh.homestay.pojo.SysOrder;
import com.yh.homestay.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private HomeStayOrderMapper homeStayOrderMapper;

    @Autowired
    private SysOrderMapper sysOrderMapper;

    @Autowired
    private OrderCarDriverMapper orderCarDriverMapper;

    @Override
    public HomeStayResult findAllOrder(Integer page, Integer limit, Integer orderType, Integer orderPayType, Long orderId) {
        Map map = new HashMap();
        map.put("orderType", orderType);
        map.put("orderPayType", orderPayType);
        map.put("orderId", orderId);
        PageHelper.startPage(page, limit);
        List<CustomSysOrder> customSysOrders = homeStayOrderMapper.selectAllOrder(map);
        PageInfo info = new PageInfo(customSysOrders);
        return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
    }

    @Override
    public HomeStayResult addOrder(SysOrder order, Date time) {
        int add = sysOrderMapper.insertSelective(order);
        if (add == 1) {
            Long orderId = order.getOrderId();
            int i = Integer.parseInt(orderId.toString());
            OrderCarDriver ocd = new OrderCarDriver();
            ocd.setOcdId(i);
            ocd.setCarId("9999");
            ocd.setDriverId(33);
            int insert = orderCarDriverMapper.insert(ocd);
            if (insert == 1) {
                Send se = new Send();
                try {
                    se.sendCode("15839721694", "杨文达", "13102856616", "云和数据");
                } catch (ClientException e) {
                    e.printStackTrace();
                }
                return HomeStayResult.ok();
            } else {
                return HomeStayResult.build(500, "出错了");
            }
        } else {
            return HomeStayResult.build(500, "出错了");
        }
    }
}